
//实用工具函数
export default class Util
{
    //生成随机数, [1, n]
    public static randNumber(n)
    {
        return Math.floor(Math.random() * n + 1)
    }

    //生成范围随机数 [m, n]
    public static randRangeNumber(m, n)
    {
        let range = n - m;
        return m + Math.round(Math.random() * range);
    }

    //范围随机数[m,n], 排除指定值e
    public static randRangeExclude(m, n, e)
    {
        if (m === n)
            return m;
        let i = Util.randRangeNumber(m, n);
        while (i === e)
        {
            i = Util.randRangeNumber(m, n);
        }
        return i;
    }

    //生成范围内的随机数组  range:什么范围内取  num:取多少个
    public static randExtract(range, num, sort = false)
    {
        let array = [];
        while (array.length < num)
        {
            let n = Util.randNumber(range);
            let found = false;
            for (let i = 0; i < array.length; i++)
            {
                if (array[i] === n)
                {
                    found = true;
                    break;
                }
            }
            if (found === false)
                array.push(n);
        }
        if (sort)
            array.sort(function compare(a, b)
            {
                return a - b
            });
        return array;
    }

    //角度转弧度
    public static toRadian(d: number)
    {
        return d * Math.PI / 180;
    }
}